// @algorithm @lc id=100326 lang=cpp 
// @title liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof


// #define print(...)
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
// (A=[4,1,8],B=[5,0,1,8],common=[4,5])=8
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        auto pa = headA, pb = headB;
        while(pa || pb){
            if(pa == pb) return pb;
            pa = pa ? pa->next : headB;
            pb = pb ? pb->next : headA;
        }
        return NULL;
    }
};